package com.example.carbonalgorithm.common.entity;


import com.example.carbonalgorithm.common.util.StringUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;

/**
 * 返回实体
 */
@Data
@Accessors(chain = true)
@ApiModel(description = "响应类")
public class Result<T> {

    /**
     * 状态
     */
    @ApiModelProperty(value = "响应码", example = "200成功 401失败")
    private Integer state;
    /**
     * 消息
     */
    @ApiModelProperty(value = "消息")
    private String msg;
    private String ssid;
    /**
     * 内容
     */
    @ApiModelProperty(value = "结果集")
    private T data;
    //    成功 状态
    public static final Integer SUCCESS_SATE = 200;
    //    保存、查询失败 状态
    public static final Integer FAIL_SATE = 401;
    //    验证码错误或失效
    public static final Integer CODE_FAIL_SATE = 402;
    //    登录失效
    public static final Integer LOGIN_SATE = 201;
    //    报错 状态
    public static final Integer ERROR_SATE = 500;
    //    数据验证失败 状态
    public static final Integer DATA_FAIL_SATE = 501;
    //    成功 返回原因
    public static final String SUCCESS_MSG = "success";
    //    保存、查询失败 状态
    public static final String FAIL_MSG = "fail";
    //    未登录 消息
    public static final String LOGIN_MSG = "Login failed";
    //    报错 状态
    public static final String ERROR_MSG = "error";
    //    数据验证失败 状态
    public static final String DATA_FAIL_MSG = "Data validation failed";
    public static final String CODE_FAIL_MSG = "验证码错误或失效";


    /**
     * 成功
     */
    public static <T> Result<T> success(T data) {
        Result<T> result = new Result<>();
        result.setMsg(SUCCESS_MSG);
        result.setState(SUCCESS_SATE);
        result.setData(data);
        return result;
    }

    public static <T> Result<T> success(String msg) {
        Result<T> result = new Result<>();
        if (StringUtil.isEmpty(msg))
            msg = SUCCESS_MSG;
        result.setMsg(msg);
        result.setState(SUCCESS_SATE);
        return result;
    }

    public static <T> Result<T> fail() {
        Result<T> result = new Result<>();
        result.setMsg(FAIL_MSG);
        result.setState(FAIL_SATE);
        return result;
    }

    public static <T> Result<T> fail(String error) {
        Result<T> result = new Result<>();
        result.setMsg(error);
        result.setState(FAIL_SATE);
        return result;
    }

    public static <S> Result<String> fail(String msg, String data) {
        Result<String> result = new Result<>();
        result.setMsg(msg);
        result.setData(data);
        result.setState(FAIL_SATE);
        return result;
    }

    public static <T> Result<T> codefail(String error) {
        Result<T> result = new Result<>();
        if (error != null)
            result.setMsg(error);
        else result.setMsg(CODE_FAIL_MSG);
        result.setState(CODE_FAIL_SATE);
        return result;
    }

    public static <T> Result<T> codefail() {
        Result<T> result = new Result<>();
        result.setMsg(CODE_FAIL_MSG);
        result.setState(CODE_FAIL_SATE);
        return result;
    }

    public static <T> Result<T> loginOut() {
        Result<T> result = new Result<>();
        result.setMsg(LOGIN_MSG);
        result.setState(LOGIN_SATE);
        return result;
    }

    public static <T> Result<T> error() {
        Result<T> result = new Result<>();
        result.setMsg(ERROR_MSG);
        result.setState(ERROR_SATE);
        return result;
    }

    public static <T> Result<T> error(String error) {
        Result<T> result = new Result<>();
        result.setMsg(error);
        result.setState(FAIL_SATE);
        return result;
    }

    public static <T> Result<T> noAccess(String msg) {
        Result<T> result = new Result<>();
        result.setMsg(msg);
        result.setState(403);
        return result;
    }

    public static <T> Result<T> noPower() {
        Result<T> result = new Result<>();
        result.setMsg("无此权限");
        result.setState(405);
        return result;
    }
}
